﻿package com.bn.NNYY;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class LastView extends SurfaceView 
implements SurfaceHolder.Callback{
	
	MyActivity activity;
	Paint paint;//画笔
	
	MainMenuButton restartbutton;//重新开始按钮的索引
	MainMenuButton exitbutton;//退出按钮的索引
	
	float pointx;//屏幕上触控到的点
	float pointy;//屏幕上触控到的点
	
	Bitmap mainbackground;//主界面的背景图片
	Bitmap restart_button;//开始按钮图片 
	Bitmap exit_button;//退出游戏按钮图片
	
	public LastView(MyActivity activity) {
		super(activity);
		this.activity=activity;
		getHolder().addCallback(this);//注册回调接口
		
		paint=new Paint();//创建画笔
		paint.setAntiAlias(true);//打开抗锯齿
		
		initBitmap();//初始化位图资源
		//开始按钮创建对象
		restartbutton=new MainMenuButton(activity,restart_button,restart_button,5*Constant.SCREEN_WIDTH/16,5*Constant.SCREEN_HEIGHT/16);
		//退出按钮创建对象
		exitbutton=new MainMenuButton(activity,exit_button,exit_button,5*Constant.SCREEN_WIDTH/16,9*Constant.SCREEN_HEIGHT/16);
		
	}

	@Override
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		canvas.clipRect(new Rect(0,0,Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT));//只在屏幕范围内绘制图片
		canvas.drawColor(Color.WHITE);//界面设置为白色
		//绘制主界面的背景图片
		canvas.drawBitmap(mainbackground, 0, 0, paint);
		//绘制开始按钮
		restartbutton.drawSelf(canvas, paint);
		//绘制退出按钮
		exitbutton.drawSelf(canvas, paint);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		pointx=(float) event.getX();
		pointy=(float) event.getY();
		switch(event.getAction())
		{
		case MotionEvent.ACTION_DOWN://按下
			if(restartbutton.isPointInRect(pointx, pointy))
			{//跳转到游戏界面
				activity.sendMessage(1);//发送消息，进入游戏界面
			}else if(exitbutton.isPointInRect(pointx, pointy))//如果按下退出按钮
			{
				System.exit(0);//退出游戏
			}
			break;
		case MotionEvent.ACTION_UP://抬起
			
			break;
		}
		return true;
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		repaint();//绘制界面
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
	}
	//初始化图片资源
	public void initBitmap(){
		//主界面的背景图片
		mainbackground=BitmapFactory.decodeResource(this.getResources(), R.drawable.main_background);
		mainbackground=PicLoadUtil.scaleToFit(mainbackground,Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
		//开始按钮图片
		restart_button=BitmapFactory.decodeResource(this.getResources(), R.drawable.restartbutton);
		restart_button=PicLoadUtil.scaleToFit(restart_button,3*Constant.SCREEN_WIDTH/8, Constant.SCREEN_HEIGHT/8);
		//退出按钮图片
		exit_button=BitmapFactory.decodeResource(this.getResources(), R.drawable.exitbutton);
		exit_button=PicLoadUtil.scaleToFit(exit_button,3*Constant.SCREEN_WIDTH/8, Constant.SCREEN_HEIGHT/8);
	}
	
	//重新绘制画面
	public void repaint()
	{
		Canvas canvas=this.getHolder().lockCanvas();
		try
		{
			synchronized(canvas)
			{
				onDraw(canvas);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			if(canvas!=null)
			{
				this.getHolder().unlockCanvasAndPost(canvas);
			}
		}
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}

}
